package com.hmall.controller;


import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.Const;;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.SysUser;
import com.hmall.service.IMService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.BackBeforeSendInformation;
import com.hmall.vo.MessageJsonVo;
import com.hmall.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;


/**
 * @ClassName IMController
 * @Description //
 * @Author zhoutao
 * @Date 2018/11/5 10:53
 * @Version 2.0
 **/
@RestController
@RequestMapping("/im/")
@Api(value = "即时消息")
public class IMController {


    private final IMService imService;

    TokenUtil tokenUtil = new TokenUtil();

    private final JedisPool jedisPool;

    @Autowired
    public IMController(IMService imService, JedisPool jedisPool) {
        this.imService = imService;
        this.jedisPool = jedisPool;
    }

    /**
     * @Date 2018/11/5 11:00
     * @Param [token, type, message]
     * @return com.hmall.common.ServerResponse
     * @Description // 发送消息
     */
    @LoginRequired
    @PostMapping("sendMessage")
    @ApiOperation(value = "发送消息接口", notes = "用户给客服发送")
    public ServerResponse sendMessage(String token, String backUsername, String messageJsonStr) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
         return imService.sendMessage(user.getUsername(), backUsername, messageJsonStr, Const.BACK_MESSAGE_EXCHG);
    }
    @LoginRequired
    @GetMapping("checkAllRead")
    @ApiOperation(value = "设置所有消息（A对B）的设为已读")
    public ServerResponse checkAllRead(String token, String sendUsername) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return imService.checkAllRead(user.getUsername(), sendUsername);
    }
    @LoginRequired
    @PostMapping("checkSoundRead")
    @ApiOperation(value = "设置某一条语音已读")
    public ServerResponse checkSoundRead(String token, Long messageNo) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return imService.checkSoundRead(user.getUsername(), messageNo);
    }



    @ApiOperation(value = "后台给用户发送消息接口", notes = "客服给用户发送")
    @PostMapping("backSendMessage")
    public ServerResponse backSendMessage(String username, String frontUsername,  String messageJsonStr){

        return  imService.sendMessage(username, frontUsername, messageJsonStr, Const.FRONT_MESSAGE_EXCHG);
    }

    @GetMapping("backGetMessageList")
    @ApiOperation(value = "后台客服查看消息历史列表", notes = "客服查看对话列表")
    public ServerResponse<PageVo<MessageJsonVo>> getBackMessageList(String username, Integer frontUserId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){

        return imService.getMessageList(username, frontUserId, Const.BACK ,pageNum, pageSize);
    }

//    @GetMapping("superGetMessageList")
//    @ApiOperation(value = "超级管理员查看某个用户的所有消息历史")
//    public ServerResponse getSuperMessageList(Integer frontUserId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
//                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
//
//        return imService.getMessageList(null, frontUserId, 3, pageNum, pageSize);
//    }

    @LoginRequired
    @GetMapping("getMessageList")
    @ApiOperation(value = "前端用户得到历史消息列表", notes = "用户得到消息记录列表")
    public ServerResponse<PageVo<MessageJsonVo>> getMessageList(String token, Integer backUserId,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return imService.getMessageList(user.getUsername(), backUserId, Const.FRONT, pageNum, pageSize);
    }
    @LoginRequired
    @GetMapping("getInformationBeforeSend")
    @ApiOperation(value = "发送前得到信息", notes = "给PC和手机端使用")
    public ServerResponse<BackBeforeSendInformation> getInformationBeforeSend(String token) throws Exception {

        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return imService.backGetInformationBeforeSend(user.getUserId(), "front");
    }


    @GetMapping("backGetInformationBeforeSend")
    @ApiOperation("客服发送前得到信息")
    public ServerResponse<BackBeforeSendInformation> backGetInformation(Integer userId){

        return imService.backGetInformationBeforeSend(userId, "back");
    }


    @GetMapping("saveCloseTime")
    @ApiOperation("PC得到浏览器关闭时间")
    public ServerResponse saveCloseTime(String username, String type, String device){

        return imService.saveCloseTime(username, type, device);
    }



}
